﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class SachManager
    {
        public ActionResultAsList<Sach> LayDSSach()
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    ar.ObjectList = db.Saches.Include("NXB1").Include("TheLoai1").ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult ThemSach(ref Sach sach)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    db.Saches.Add(sach);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResult XoaSach(string masach)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var sach = db.Saches.Single(s => s.MaSach == masach);
                    db.Saches.Remove(sach);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }

            }
            return ar;
        }
        public ActionResult SuaSach(Sach sach)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var t = db.Saches.Single(s => s.MaSach == sach.MaSach);
                    t.MaSach = sach.MaSach;
                    t.TenSach = sach.TenSach;
                    t.TrangThai = sach.TrangThai;
                    t.NXB = sach.NXB;
                    t.TheLoai = sach.TheLoai;
                    t.TacGia = sach.TacGia;
                    t.NamXB = sach.NamXB;
                    t.SoTrang = sach.SoTrang;
                    t.Gia = sach.Gia;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }

            }
            return ar;
        }
        public ActionResultAsList<Sach> TimKiemSachTheoMa(string masach)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.MaSach.Contains(masach));

                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> TimKiemSachTheoTen(string tensach)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.TenSach.Contains(tensach));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> LaySachTheoTrangThai(string trangthai)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.TrangThai == trangthai);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> TimKiemSachTheoTacGia(string tentacgia)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.TacGia.Contains(tentacgia));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> TimKiemSachTheoNXB(string tennxb)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.NXB1.TenNXB.Contains(tennxb));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> TimKiemSachTheoTheLoai(string tentheloai)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(n => n.TheLoai1.TenTheLoai.Contains(tentheloai));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult CapNhatTrangThaiSach(string masach, string trangthaimoi)
        {
            var ar = new ActionResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var sach = db.Saches.Single(s => s.MaSach == masach);
                    sach.TrangThai = trangthaimoi;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Sach> LayDSSachNhap(int thang, int nam)
        {
            var ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(s => s.NgayNhap.Month == thang && s.NgayNhap.Year == nam);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ValidateResult KiemTraTrungMa(string masach)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var trungma = db.Saches.Where(s => s.MaSach == masach);
                    if (trungma.Any())
                        vr.AddMessage("Trung ma sach");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }        
        public ValidateResult KiemTraRangBuocKhiXoaTheLoai(string matheloai)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var rangbuoc = db.Saches.Where(c => c.TheLoai == matheloai);
                    if (rangbuoc.Any())
                        vr.AddMessage("Bi rang buoc voi bang sach");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }
        public ValidateResult KiemTraRangBuocKhiXoaNXB(string manxb)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var rangbuoc = db.Saches.Where(c => c.NXB == manxb);
                    if (rangbuoc.Any())
                        vr.AddMessage("Bi rang buoc voi bang sach");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }

    }

}
